CFAdata_2<-subset(study2, Obedience>0 & Considerate>0 & Curiosity>0 & Independence>0)
CFAdata_2$treatment2<-as.numeric(CFAdata_2$treatment2)


vars <- c("Independence", "Obedience", "Curiosity", "Considerate")

irt_data_2 <- subset(CFAdata_2, select = vars)

irt_data_2$Obedience <- 3-irt_data_2$Obedience
group_2<-as.character(CFAdata_2$treatment2 )

table(group_2)

group_2 <- dplyr::recode(group_2, "2"="Control", "1"="Boy ", "3"="Girl")

mod_configural_2<-multipleGroup( data=irt_data_2, 
                                 model=1, 
                                 group= group_2 ) 

mod_configural_2
summary(mod_configural_2)
M2(mod_configural_2)


mod_metric_2<-multipleGroup( data=irt_data_2, 
                             model=1, 
                             group= group_2, 
                             invariance=c('slopes'))

mod_metric_2
summary(mod_metric_2)
M2(mod_metric_2)


anova( mod_metric_2, mod_configural_2)
mod_metricb_2<-multipleGroup( data=irt_data_2, 
                              model=1, 
                              group= group_2, 
                              invariance=c('intercepts'))

mod_metricb_2
summary(mod_metricb_2)
M2(mod_metricb_2)


anova( mod_metricb_2, mod_configural_2)
mod_scalar2_2<-multipleGroup( data=irt_data_2, 
                              model=1, 
                              group= group_2, 
                              invariance=c('slopes', 'intercepts', 'free_var','free_means')
)



mod_scalar2_2
summary(mod_scalar2_2)
M2(mod_scalar2_2)
anova( mod_scalar2_2, mod_configural_2)


mod_scalar1_2<-multipleGroup( data=irt_data_2, 
                              model=1, 
                              group= group_2, 
                              invariance=c('slopes', 'intercepts', 'free_var')
)



mod_scalar1_2
summary(mod_scalar1_2)
M2(mod_scalar1_2)

anova( mod_scalar1_2, mod_configural_2)

mod_fullconstrain_2<-multipleGroup( data=irt_data_2, 
                                    model=1, 
                                    group= group_2, 
                                    invariance=c('slopes', 'intercepts')
)



mod_fullconstrain_2
summary(mod_fullconstrain_2)
M2(mod_fullconstrain_2)

anova( mod_fullconstrain_2, mod_configural_2)


anova(mod_metric_2, mod_configural_2) #equal slopes only
anova(mod_scalar2_2, mod_metric_2) #equal intercepts, free variance and mean
anova(mod_scalar1_2, mod_scalar2_2) #fix mean
anova(mod_fullconstrain_2, mod_scalar1_2) #fix variance


anova(mod_fullconstrain_2, mod_scalar1_2, mod_scalar2_2, mod_metric_2, mod_configural_2)

#plot(mod_configural_2, type = 'trace')

